/*****************************************************************************/
/*  gtkui.c - GTK+ UI related functions for gFTP                             */
/*  Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org>                  */
/*                                                                           */
/*  This program is free software; you can redistribute it and/or modify     */
/*  it under the terms of the GNU General Public License as published by     */
/*  the Free Software Foundation; either version 2 of the License, or        */
/*  (at your option) any later version.                                      */
/*                                                                           */
/*  This program is distributed in the hope that it will be useful,          */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
/*  GNU General Public License for more details.                             */
/*                                                                           */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program; if not, write to the Free Software              */
/*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA      */
/*****************************************************************************/

#include "common.h"

static void
_gftpui_wakeup_main_thread (gpointer data, gint source,
                            GdkInputCondition condition);

static gint
_gftpui_setup_wakeup_main_thread (gftp_request *request);

static void
_gftpui_teardown_wakeup_main_thread (gftp_request *request, gint handler);

void
gftpui_lookup_file_colors (gftp_file * fle, char **start_color,
                           char ** end_color)
{
  *start_color = GFTPUI_COMMON_COLOR_NONE;
  *end_color = GFTPUI_COMMON_COLOR_NONE;
}

void
gftpui_run_command (GtkWidget * widget, gpointer data)
{
}

void
gftpui_refresh (void *uidata, int clear_cache_entry)
{
}


void
gftpui_show_busy (gboolean busy)
{
}

void
gftpui_prompt_username (void *uidata, gftp_request * request)
{
}

void
gftpui_prompt_password (void *uidata, gftp_request * request)
{
}

typedef struct thread_data
{
  void * (*func) (void *);
  void *data;
} thread_data;


void *
_gftpui_gtk_thread_func (void *data)
{
  void *ret;
  thread_data *tdata;
  tdata = data;
 
  ret = tdata->func (tdata->data);


  if (right_viewer->request->wakeup_main_thread[1] > 0)
    write (right_viewer->request->wakeup_main_thread[1], " ", 1);

  return (ret);
}

void *
gftpui_generic_thread (void * (*func) (void *), void *data)
{
    gint handler;
    void * ret;
    gftp_request *request = right_viewer->request;
    thread_data *tdata;
    
    tdata = g_malloc0 (sizeof (*tdata));
    tdata->func = func;
    tdata->data = data;
    
    request->running = 1;
    gtk_widget_set_sensitive (gftp_interface->stop_btn, 1);

    handler = _gftpui_setup_wakeup_main_thread (request);
    pthread_create (&right_viewer->tid, NULL, _gftpui_gtk_thread_func, tdata);

    while (request->running) {
        gdk_threads_leave ();  
        g_main_context_iteration (NULL, TRUE);
        gdk_threads_enter ();
    }

    _gftpui_teardown_wakeup_main_thread (request, handler);

    pthread_join (right_viewer->tid, &ret);

    g_free (tdata);
    gtk_widget_set_sensitive (gftp_interface->stop_btn, 0);
    
    if (!GFTP_IS_CONNECTED (request))
        gftpui_disconnect (NULL);

    return (ret);
}

/* int */
/* gftpui_check_reconnect (gftpui_callback_data * cdata) */
/* { */
/*   return 0; */
/* } */

/*
void
gftpui_run_function_callback (gftp_window_data * wdata,
                              gftp_dialog_data * ddata)
{

}
*/

 /*
void
gftpui_run_function_cancel_callback (gftp_window_data * wdata,
                                     gftp_dialog_data * ddata)
{
}
 */

void
gftpui_mkdir_dialog (gpointer data)
{
}

void
gftpui_rename_dialog (gpointer data)
{
 
}

void
gftpui_site_dialog (gpointer data)
{

}

int
gftpui_run_chdir (gpointer uidata, char *directory)
{
  return 0;
}

void
gftpui_chdir_dialog (gpointer data)
{
}

void 
gftpui_disconnect (void *uidata)
{

}

char *
gftpui_gtk_get_utf8_file_pos (gftp_file * fle)
{
  char *pos;

  if ((pos = strrchr (fle->file, '/')) != NULL)
    pos++;
  else
    pos = fle->file;

  return (pos);
}

int
gftpui_protocol_ask_yes_no (gftp_request * request, char *title,
                            char *question)
{
    GtkWidget *dialog, *label, *image, *hbox;
    int result;

    dialog = gtk_dialog_new_with_buttons (title, 
                                   GTK_WINDOW (gftp_interface->window), 
                                   GTK_DIALOG_MODAL,
                                   _("YES"), 1,
                                   _("NO"), 0,
                                   NULL);

    label = gtk_label_new (question);
    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
    gtk_widget_set_size_request(label, 400, -1);
    image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_INFO,
                                      GTK_ICON_SIZE_DIALOG);
  
    hbox = gtk_hbox_new (FALSE, 5);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
    gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
    gtk_box_pack_start_defaults (GTK_BOX (hbox), label);

    gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG(dialog)->vbox), hbox);
    gtk_widget_show_all (dialog);

    result = gtk_dialog_run (GTK_DIALOG (dialog));

    gtk_widget_destroy (dialog);

    return result;
}


char *
gftpui_protocol_ask_user_input (gftp_request * request, char *title,
                                char *question, int shown)
{
    return (NULL);
}

/* The wakeup main thread functions are so that after the thread terminates
   there won't be a delay in updating the GUI */
static void
_gftpui_wakeup_main_thread (gpointer data, gint source,
                            GdkInputCondition condition)
{
  gftp_request * request;
  char c;

  request = data;
  request->running = 0;
  if (request->wakeup_main_thread[0] > 0)
    read (request->wakeup_main_thread[0], &c, 1);
}


static gint
_gftpui_setup_wakeup_main_thread (gftp_request *request)
{
  gint handler;

  if (socketpair (AF_UNIX, SOCK_STREAM, 0, request->wakeup_main_thread) == 0)
    {
      handler = gdk_input_add (request->wakeup_main_thread[0],
                               GDK_INPUT_READ, _gftpui_wakeup_main_thread,
                               request);
    }
  else
    {
      request->wakeup_main_thread[0] = 0;
      request->wakeup_main_thread[1] = 0;
      handler = 0;
    }

  return (handler);
}


static void
_gftpui_teardown_wakeup_main_thread (gftp_request *request, gint handler)
{
  if (request->wakeup_main_thread[0] > 0 && request->wakeup_main_thread[1] > 0)
    {
      gdk_input_remove (handler);
      close (request->wakeup_main_thread[0]);
      close (request->wakeup_main_thread[1]);
      request->wakeup_main_thread[0] = 0;
      request->wakeup_main_thread[1] = 0;
    }
}
